package com.cloud.sys.service.impl;

import com.cloud.sys.entity.SysUser;
import com.cloud.sys.mapper.SysUserMapper;
import com.cloud.sys.service.SysUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.codec.digest.DigestUtils;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (SysUser)表服务实现类
 *
 * @author makejava
 * @since 2018-11-04 11:09:35
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    @Cacheable(value = "user", key = "#id", unless = "#result.id eq ''")
    public SysUser queryById(String id) {
        SysUser user=this.sysUserMapper.queryById(id);
        return user;
    }
    /**
     * @projectName: SysUserMapper
     * @Description: 通过userName 进行查询
     * @author zxh
     * @date 2018/11/18 16:45
     */
    @Override
    @Cacheable(value = "user", key = "#name", unless = "#result.id eq ''")
    public SysUser queryByName(String name) {
        SysUser user=this.sysUserMapper.queryByName(name);
        return user;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    @Cacheable(value = "user", key = "#user.userName+user.realName+offset+limit", unless = "#result.size eq 0")
    public PageInfo<List<SysUser>> queryAllByLimit(int offset, int limit, SysUser user) {
        Page<List<SysUser>> page= PageHelper.startPage(offset,limit);
        this.sysUserMapper.queryAll(user);
        PageInfo <List<SysUser>> pageInfo=page.toPageInfo();
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    @CachePut(value = "user", key = "#sysUser.id" )
    public Integer insert(SysUser sysUser) {
        return   this.sysUserMapper.insert(sysUser);
    }

    /**
     * 修改数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    @CachePut(value = "user", key = "#sysUser.id" )
    public Integer update(SysUser sysUser) {
        return this.sysUserMapper.update(sysUser);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @CacheEvict(value = "user", key = "#id" )
    public boolean deleteById(String id) {
        return this.sysUserMapper.deleteById(id) > 0;
    }


    /**  通过用户名+密码 查询
     * @projectName: SysUserService
     * @Description: TODO
     * @author zxh
     * @date 2018/11/4 11:12
     */
    @Override
    public SysUser login(SysUser user) {
        user.setPassword(DigestUtils.md5Hex(user.getPassword()));
        List<SysUser> list=sysUserMapper.queryAll(user);
        return list.size()>0?list.get(0):user;
    }
}